// 用户表
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema
const UserSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  avatar: {
    type: String
  },
  identity: {// 身份
    type: String,
  },
  now_map: {
    type: Object,
    ref: "maps",
  },
  strength: {// 力量属性
    type: Number,
    required: true,
    default: 0
  },
  defense: {// 体质属性
    type: Number,
    required: true,
    default: 0
  },
  magic: {// 灵力属性
    type: Number,
    required: true,
    default: 0
  },
  speed: { // 速度属性
    type: Number,
    required: true,
    default: 0
  },
  user_exp: { // 灵气值
    type: Number,
    required: true,
    default: 0
  },
  hp: { // 当前生命值
    type: Number,
    required: true,
    default: 100
  },
  max_hp: { // 最大生命值
    type: Number,
    required: true,
    default: 100
  },
  energy: { // 当前体力值
    type: Number,
    required: true,
    default: 100
  },
  max_energy: { // 最大体力值
    type: Number,
    required: true,
    default: 100
  },
  nerve: { // 闲心值
    type: Number,
    required: true,
    default: 20
  },
  status: { // 当前状态 (0正常，1受伤中)
    type: Number,
    required: true,
    default: 0
  },
  created_at: {     //创建时间
    type: Date,
    default: Date.now()
  },
  register_ip: {//注册ip
    type: String
  },

});

UserSchema.virtual('info').get(function () {
  return {
    _id: this._id,
    now_map: this.now_map,
    strength: this.strength,
    defense: this.defense,
    magic: this.magic,
    speed: this.speed,
    user_exp: this.user_exp,
    hp: this.hp,
    max_hp: this.max_hp,
    nerve: this.nerve
  }
});




module.exports = User = mongoose.model('users', UserSchema);
